<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客注册页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
<div class="nav">
    <img src="pic/smarmot.png" alt="" />
    <span class="blog-title">我的博客系统</span>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
</div>

<div class="container-login">
    <div class="login-dialog">
        <h3>注册</h3>
        <div class="row">
            <span>用户名:</span>
            <input type="text" name="username" id="username">
        </div>
        <div class="row">
            <span>密码:</span>
            <input type="password" name="password" id="password">
        </div>
        <div class="row">
            <span>确认密码:</span>
            <input type="password" name="confirmPassword" id="confirmPassword">
        </div>
        <div class="row">
            <span>邮箱:</span>
            <input type="text" name="email" id="email">
        </div>
        <div class="row">
            <button id="getVerificationCode" onclick="sendCaptcha()">获取验证码</button>
            <input type="text" name="captcha" id="captcha" placeholder="输入验证码">
        </div>
        <div class="row">
            <button id="submit" onclick="register()">注册</button>
        </div>

    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    function sendCaptcha() {
        var email = $("#email").val();
        $.ajax({
            type: "post",
            url: "/user/generateCaptcha",
            data: {
                email: $("#email").val(),
            },
            success: function(result){
                if(result.code == "SUCCESS" && result.data != null){
                    alert("验证码已发送至邮箱！");
                    localStorage.setItem("register_captcha", result.data.codeKey);
                }else{
                    alert(result.errorMessage);
                }
            }
        });
    }

    function register() {

    $.ajax({
        type: "POST",
        url: "/user/register",
        contentType: "application/json",  
        data: JSON.stringify({            // 将数据转化为 JSON 字符串
            userName: $("#username").val(),
            password: $("#password").val(),
            confirmPassword: $("#confirmPassword").val(),
            email: $("#email").val(),
            captcha: $("#captcha").val()
            }),
        headers: {
            "register_captcha": localStorage.getItem("register_captcha")
        },
        success: function(result){
            if(result.code === "SUCCESS" && result.data === ""){
                alert("注册成功");
                location.href = "blog_login.html";
            } else {
                alert(result.errorMessage);
            }
        }
    });
}
</script>
</body>

</html>
